/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author Asus
 */
public class DataBaseConnection {
//    private String jdbcURL = "jdbc:oracle:thin:@localhost:1521:xe";
//    private String user = "hr";
//    private String password = "12345";
    private String jdbcURL = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    private String user = "MHS115314009";
    private String password = "MHS115314009";
//    private String user = "reynusa";
//    private String password = "reynusa"; // just for DB komp Reynusa
    private Connection connection = null;

    /** Creates a new instance of UserConnection */
    public DataBaseConnection() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(jdbcURL, user, password);
            System.out.println("koneksi berhasil");
        } catch (Exception ex) {
            System.out.println("message: " + ex.getMessage());
        }
    }
    
    public static Connection open(String url, String userId, String password) {
        Connection conn = null;
        try {
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(url);
            conn = ds.getConnection(userId, password);
            System.out.println("Koneksi is OK");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
        return conn;
    }
    
    public static void main(String[] args) {
        DataBaseConnection.lihatstok();
        
    }

    public boolean isConnected() {
        if (connection != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getClosed() {
        if (isConnected()) {

            try {
                connection.close();
                connection = null;
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }

    public String getJdbcURL() {
        return jdbcURL;
    }

    public void setJdbcURL(String jdbcURL) {
        this.jdbcURL = jdbcURL;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConn(Connection connection) {
        this.connection = connection;
    }

    public static void lihatstok() {
//        Connection conn=DataBaseConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "12345");
        Connection conn=DataBaseConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs115314009", "mhs115314009");
//          Connection conn=DataBaseConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "reynusa", "reynusa");
        java.sql.Statement statement = null;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sqQ = "select * from penjualantoko";
        ResultSet result = null;
        try {
            result = statement.executeQuery(sqQ);
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while (result.next()) {
                PenjualanToko temp = new PenjualanToko();
                temp.setKodebarang(result.getString("kode_barang"));
//                temp.setJumlah(result.getString("jumlah"));
                temp.setTgl_jual(result.getString("tgl_beli"));
//               s System.out.println("kode_toko : " + temp.getKodetoko());
                System.out.println("kode_barang : " + temp.getKodebarang());
                System.out.println("jumlah : " + temp.getJumlah());
                System.out.println("tgl jual : " + temp.getTgl_jual());
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            conn.close();
        } catch (SQLException ex) {
        }
    }

    
    }





